package Autodesk;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

 
public class WebservReader {
 
	@SuppressWarnings("finally")
	public HashMap<String, String> readXml(String productTag,String releaseTag,String defaultTag,String param){
		String result=null;
		
		HashMap<String, String> ht= new HashMap<String, String>();
		ht.put("e", null);
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder;
		try {
			dBuilder = dbFactory.newDocumentBuilder();
			Document doc;
	
			NodeList nList  = defineURL(productTag, releaseTag, defaultTag, param, dBuilder);
			//System.out.println(nList.getLength());
			for (int temp = 0; temp < nList.getLength(); temp++) {
			   Node nNode = nList.item(temp);
			   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
			      getValue(releaseTag, param, ht, nNode);
			   }
			}
		} catch (Exception e1) {	
			//System.out.println("error!");
			ht.put("e", "Error!");
		}finally{
		  return ht;
		}
	 
  }

	private NodeList defineURL(String productTag, String releaseTag,
			String defaultTag, String param, DocumentBuilder dBuilder)
			throws SAXException, IOException, MalformedURLException {
		Document doc;
		NodeList nList=null;
		if("products".equals(param) || "product".equals(param) ){
			if( ( releaseTag.equals("all")|| releaseTag.equals("") ) && !productTag.equals("")){
				doc = dBuilder.parse(new URL("http://184.72.16.147/toolkit/service/rest/resource/taxonomy3/baseline?p="+productTag+""+
																					"&origin=upi").openStream());
				nList = doc.getElementsByTagName("Baseline");

			}else if(productTag.equals("")){
				defaultTag=URLEncoder.encode(defaultTag, "UTF-8");
				doc = dBuilder.parse(new URL("http://184.72.16.147/toolkit/service/rest/resource/taxonomy3/baseline?p="+defaultTag+"&v=all&origin=baseline").openStream());
				nList = doc.getElementsByTagName("Baseline");
			}else{
				doc = dBuilder.parse(new URL("http://184.72.16.147/toolkit/service/rest/resource/taxonomy3/baseline?p="+productTag+"" +
						"&v="+releaseTag+"&origin=upi").openStream());	
				nList = doc.getElementsByTagName("Baseline");
			}
		}else if("language".equals(param) || "languages".equals(param) ){
			doc = dBuilder.parse(new URL("http://184.72.16.147/toolkit/service/rest/resource/taxonomy3/baseline?l="+defaultTag+"" +
																											"&origin=upi").openStream());
			nList = doc.getElementsByTagName("Baseline");
		}else{
			  doc = dBuilder.parse(new URL("http://184.72.16.147/toolkit/service/rest/resource/taxonomy3/channelname?c="+defaultTag+
						"&origin=upi").openStream());
				nList = doc.getElementsByTagName("ChannelService");

		}
		return nList;
	}

	private void getValue(String releaseTag, String param,
			HashMap<String, String> ht, Node nNode) {
		Element eElement = (Element) nNode;
		  if("products".equals(param) || "product".equals(param) ){
			  ht.put("p", getTagValue("productLineBaseline", eElement));
			  if((releaseTag.equals("all")) || releaseTag.equals("")){
				  ht.put("r", "all");
			  }else{
				  ht.put("r",getTagValue("productReleaseBaseline", eElement));
			  }
			 //System.out.println(getTagValue("productLineBaseline", eElement));
		  }else if("language".equals(param) || "languages".equals(param) ){
		       ht.put("d", getTagValue("languageBaseline", eElement)); 
			   //System.out.println(getTagValue("languageBaseline", eElement));
		  }else{
		       ht.put("d", getTagValue("channelName", eElement));  
		  }
	}
 
  private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
 
        Node nValue = (Node) nlList.item(0);
 
	return nValue.getNodeValue();
  }
  // "http://184.72.16.147/toolkit/service/rest/resource/taxonomy3/baseline?p=MAYAUL&v=2013&l=ENU&origin=upi"
  
  
  public static void main(String[] args) {
		HashMap<String, String> ht = new HashMap<String, String>();
		WebservReader xmlR=new WebservReader();
		ht=xmlR.readXml("ACDLT","2013", "","products");
		System.out.println("----------------------------------");
		ht=xmlR.readXml("REVIT","all", "","products");
		System.out.println("----------------------------------");
		ht=xmlR.readXml("ACDLT","all", "","products");
		System.out.println("----------------------------------");

//		ht=xmlR.readXml("","2014", "REVIT","products");
//		ht=xmlR.readXml("","all", "AutoCAD Raster Design","products");
//		ht=xmlR.readXml("","all", "의 AutoCAD","products");
//		ht=xmlR.readXml("","all", "Inventeur","products");
		ht = xmlR.readXml("", "", "acadconnectchannel3", "channels");
		System.out.println(ht.get("d"));
//		ht = xmlR.readXml("MAYAUL", "2013", "", "products");
//		ht = xmlR.readXml("CWARE", "Help", "", "products");
//		ht = xmlR.readXml("", "", "enu", "languages");
//		ht = xmlR.readXml("", "", "acadconnectchannel", "channels");
//		ht = xmlR.readXml("MAYAU", "2013", "", "products");
//		ht = xmlR.readXml("", "", "eu", "languages");

   //  xmlR.readXmanObjectl("AutoCAD Mechanical","AutoCAD Mechanical 2010","languageBaseline","products");
}
  
  
}